#include "form_mode_verify_dlg.h"
#include "ui_form_mode_verify_dlg.h"
#include <QTranslator>
#include <QTimer>

#include "utils/keyboard/keyboard_mgr.h"
#include "utils/algorithm/algorithm_bll_t1c.h"
#include "utils/firmware/firmware_bll.h"
#include "utils/database/database_bll_t1c.h"
#include "ui/share/qlineeditpwd.h"

static QString gs_sModeNameOld = "";

/*
 * Function:    FormModeVerifyWidget
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormModeVerifyWidget::FormModeVerifyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormModeVerifyWidget)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);

    m_pLineEditPwd = NULL;
}

/*
 * FormModeVerifyWidget:    ~FormModeVerifyWidget
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormModeVerifyWidget::~FormModeVerifyWidget()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormModeVerifyWidget" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormStartupTest" class
 */
FormModeVerifyWidget* FormModeVerifyWidget::getInstance(QWidget *pParent)
{
    static FormModeVerifyWidget *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormModeVerifyWidget(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_Initialze();

        // Initialize control's UI
        pNew->_InitUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormModeVerifyWidget::_Initialze()
{
    qDebug() << Q_FUNC_INFO;

    if(!m_pLineEditPwd) m_pLineEditPwd = new QLineEditPwd(this);
    Q_ASSERT(NULL != m_pLineEditPwd);
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormModeVerifyWidget::_InitUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/setting/%2.qss").arg("skin_1").arg(this->objectName()));

    m_pLineEditPwd->setMinimumHeight(50);
    ui->gridLayout->addWidget(m_pLineEditPwd, 0, 1);
    ui->label_PromtInfo->setText("");
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormModeVerifyWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);

    switch (e->type())
    {
    case QEvent::LanguageChange:
        qDebug() << Q_FUNC_INFO;

//        ui->label_Pwd->setText(tr("Password:"));
//        ui->pushButton_Confirm->setText(tr("Confirm"));
//        ui->pushButton_Cancel->setText(tr("Cancel"));

        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

/*
* Function:		on_pushButton_Save_clicked
* Description:	Save the basic info after click the button
* Paramer:		void
* Return:		void
*/
void FormModeVerifyWidget::on_pushButton_Confirm_clicked()
{
    qDebug() << Q_FUNC_INFO;

    bool bCon = false;
    int nRetValue = 0;
    QString sModeName = ui->label_PageTitle->text().trimmed();
    QString sModePwd = m_pLineEditPwd->text().trimmed();
    QString sPwdAesUi = AlgorithmBLL_T1C::getInstance(this)->encryptionAES(sModePwd);
    QString sPwdAesDb = QString::number(QDateTime::currentDateTime().toString("yyyyMMdd").toInt(), 10);

#ifndef QT_NO_DEBUG
    sPwdAesDb = "";
#endif
    if(tr("Admin Mode") == sModeName) {
        DatabaseBLL_T1C::getInstance(this)->selectUserPwd(DEF_ACCOUNT_ADMIN, sPwdAesDb);
//        sPwdAesDb = QString("adm") + sPwdAesDb;

        if(sPwdAesDb != sPwdAesUi) {
            ui->label_PromtInfo->setText(tr("password not match"));
            goto do_error;
        }
        else {
            nRetValue = 1;
        }
    }
    else if(tr("Factory Mode") == sModeName) {
        DatabaseBLL_T1C::getInstance(this)->selectUserPwd(DEF_ACCOUNT_FACTORY, sPwdAesDb);
//        sPwdAesDb = QString("fac") + sPwdAesDb;

        if(sPwdAesDb != sPwdAesUi) {
            ui->label_PromtInfo->setText(tr("password not match"));
            goto do_error;
        }
        else {
            nRetValue = 2;
        }
    }
    else {
        this->hide();
    }

    // Signal-slot initialization
    bCon = QObject::connect(this, SIGNAL(signalReplayInfo(int, bool)), this->parent(), SLOT(slotRespondInfoFromDlg(int, bool)), Qt::QueuedConnection);
    Q_ASSERT(bCon);

    emit signalReplayInfo(nRetValue, true);
    this->hide();

do_error:
    // Display password error message
    QTimer::singleShot(3000, this, [&](){
        ui->label_PromtInfo->setText("");
    });

    qDebug() << Q_FUNC_INFO << sModeName << sModePwd;
}

/*
* Function:		on_pushButton_Cancel_clicked
* Description:	Close the window after click the button
* Paramer:		void
* Return:		void
*/
void FormModeVerifyWidget::on_pushButton_Cancel_clicked()
{
    qDebug() << Q_FUNC_INFO;

    m_pLineEditPwd->setText("");

    // Signal-slot initialization
    bool bCon = QObject::connect(this, SIGNAL(signalReplayInfo(int, bool)), this->parent(), SLOT(slotRespondInfoFromDlg(int, bool)), Qt::QueuedConnection);
    Q_ASSERT(bCon);

    int nRetValue = 0;
    QString sModeName = ui->label_PageTitle->text().trimmed();

    if(tr("Admin Mode") == gs_sModeNameOld) {
        nRetValue = 1;
    }
    else if(tr("Factory Mode") == gs_sModeNameOld) {
        nRetValue = 2;
    }
    else {
        nRetValue = 0;
    }

    emit signalReplayInfo(nRetValue, false);
    this->hide();
}

/*
 * Function:    showModeVerifyDialog
 * Description: Display the mode verify dialog
 * Paramer:     QString sModeName - the mode name
 * Return:      void
 */
void FormModeVerifyWidget::showModeVerifyDialog(QString sModeNameNew, QString sModeNameOld)
{
    qDebug() << Q_FUNC_INFO << sModeNameNew << sModeNameOld;
    gs_sModeNameOld = sModeNameOld;

    // Update the prompt info
    ui->label_PageTitle->setText(sModeNameNew);

    m_pLineEditPwd->setText("");

    KeyboardMgr::getInstance(this)->setParent(this);

    this->show();
}
